/**
 * 道具类
 */
class prop extends eui.Component {
	public constructor(game) {
		super();
		this.gameHandle = game;
	}

	private gameHandle;

	/**道具类型 */
	public propType;
	private c = serverConfig.config;
	/**道具显示对象 */
	public propSprit: eui.Image;
	public isUsed: boolean;
	public propId = -1;
	public tweenover: egret.Point;
	public tweenstart: egret.Point;
	/**是否在视野范围内， */
	protected isInRange: boolean;

	private leafimg;
	private isInTween;
	/**道具初始化 */
	public propInit() {
		this.propSprit = new eui.Image();
		this.addChild(this.propSprit);
		this.propSprit.x = this.x = 0;
		this.propSprit.y = this.y = 0;
		this.tweenover = new egret.Point(0, 0);
		this.tweenstart = new egret.Point(0, 0);
		this.propSprit.scaleX = this.propSprit.scaleY = gameGlobal.global.propsScal;
		this.visible = this.isUsed = false;
		this.isInRange = true;
		this.leafimg = RES.getRes("invite_adorn1");
		this.isInTween = false;
	}
	public update() {
		this.checkInRange();
	}
	public propBron(resName, x, y, id) {
		this.x = x;
		this.y = y;
		this.propId = id;
		this.propSprit.texture = RES.getRes(resName);
		this.propSprit.anchorOffsetX = this.propSprit.width / 2;
		this.propSprit.anchorOffsetY = this.propSprit.height / 2;
		this.propSprit.scaleX = this.propSprit.scaleY = 1;
		//this.effectOfProps(Math.floor(Math.random() * 10 + 0));
		this.gameHandle.displayProps.addChild(this);
		this.visible = this.isUsed = true;
		this.isInTween = false;
	}
	/**随机生成的荷叶 */
	public leafBron(x, y, id, grade) {
		this.x = x;
		this.y = y;
		this.propId = id;
		this.propSprit.texture = this.leafimg;
		this.propSprit.anchorOffsetX = this.propSprit.width / 2;
		this.propSprit.anchorOffsetY = this.propSprit.height / 2;
		this.leafsize(this, grade);
		//this.effectOfProps(Math.floor(Math.random() * 10 + 0));
		this.gameHandle.displayProps.addChild(this);
		this.visible = this.isUsed = true;
	}
	/**生成荷叶并播放动画 */
	public leafBronTween(overx, overy, id, startx, starty, grade) {

		let tmp = this;
		if (this.isInTween) {
			return;
		}
		this.isInTween=true;
		//清空所有动画
		egret.Tween.removeTweens(this);
		//记录荷叶开始和结束位置 用于判断是否是同一个荷叶
		this.tweenstart.x = startx;
		this.tweenstart.y = starty;
		this.tweenover.x = overx;
		this.tweenover.y = overy;
		//将荷叶当前位置设置为动画开始位置
		this.x = this.tweenstart.x;
		this.y = this.tweenstart.y;
		this.propId = id;
		this.propSprit.texture = this.leafimg;
		this.propSprit.anchorOffsetX = this.propSprit.width / 2;
		this.propSprit.anchorOffsetY = this.propSprit.height / 2;
		//设置荷叶大小
		this.leafsize(this, grade);
		this.gameHandle.displayProps.addChild(this);
		this.visible = this.isUsed = true;
		egret.Tween.get(this).to({ x: overx, y: overy }, 500).call(e => { tmp.isInTween=false; });

	}

	public setInRange(isRange) {
		this.isInRange = isRange;

	}
	protected checkInRange() {

		if (DataProl.isXinShou) {
			return;
		}
		// if (this.isInRange == this.isInRangeLast) {
		// 	return;
		// }
		// else {
		this.checkProp();
		this.visible = this.isInRange;

		// }
	}
	protected checkProp() {
		let my = webNetworkMananger.getInstance().gameSceneRec.m_createAllFrogs.findFrogById(DataProl.account)
		if (my == null || my == undefined) { return; }
		let rangeEye = this.c.rangeEye;
		if (!my.deathDate) {
			if (MathUtils.distance(this.x, this.y, my.x, my.y) < rangeEye) {
				this.isInRange = true;
			}
			else {
				this.isInRange = false;
			}
		}
	}
	/**荷叶大小判断 */
	public leafsize(leaf, grade) {
		if (grade == 2) {
			this.propSprit.scaleX = this.propSprit.scaleY = 0.5;
		}
		if (grade == 5) {
			this.propSprit.scaleX = this.propSprit.scaleY = 0.7;
		}

		if (grade == 20) {
			this.propSprit.scaleX = this.propSprit.scaleY = 0.9;
		}

		if (grade >= 50) {
			this.propSprit.scaleX = this.propSprit.scaleY = 1.3;
		}
	}
	public propRecover() {
		this.visible = this.isUsed = false;
		this.gameHandle.displayProps.removeChild(this);
		this.propId = -1;
		this.propSprit.texture = null;
		this.x = this.y = 0;
		egret.Tween.removeTweens(this.propSprit);
		this.isInTween = false;
	}
    /**道具显示效果 
	 * 0 顺时针旋转
	 * 1 逆时针旋转
	*/
	private effectOfProps(type) {
		var scal = gameGlobal.global.propsScal;
		egret.Tween.get(this.propSprit, { loop: true }).to({ rotation: (type > 5 ? 359 : -359) }, 36000);
		egret.Tween.get(this.propSprit, { loop: true }).to({ scaleX: scal - 0.1, scaleY: scal - 0.1 }, 1000).to({ scaleX: scal, scaleY: scal }, 1000);
	}


}